export function formatRouters(menuArray: any[]): any {
    const flatMenu = [];
    let idCounter = 1;
    const queue = menuArray.map(item => ({ ...item, pid: null })); // 初始化队列并设置顶级菜单的pid为null

    while (queue.length > 0) {
        const currentItem = queue.shift(); // 从队列中取出当前元素
        const { children, ...itemWithoutChildren } = currentItem;
        itemWithoutChildren.id = idCounter++; // 分配ID
        flatMenu.push(itemWithoutChildren); // 添加到扁平菜单数组

        if (children && children.length) {
            children.forEach((child: any) => {
                queue.push({ ...child, pid: itemWithoutChildren.id }); // 将子项添加到队列，设置其pid
            });
        }
    }
    const rows: any = [
        {
            'name': 'home',
            'path': '/home',
            'meta.title': '首页',
            'meta.requiresAuth': true,
            'meta.icon': 'icon-park-outline:analysis',
            'meta.pinTab': false,
            'meta.menuType': 'page',
            'componentPath': '/home/index.vue',
            'id': 0,
            'pid': null,
        },
        {
            'name': 'list',
            'path': '/list',
            'meta.title': '示例',
            'meta.requiresAuth': true,
            'meta.icon': 'icon-park-outline:list-two',
            'meta.menuType': 'dir',
            'componentPath': null,
            'id': 101,
            'pid': null,
        },
        {
            'name': 'commonList',
            'path': '/list/commonList',
            'meta.title': '用户管理',
            'meta.requiresAuth': true,
            'meta.icon': 'icon-park-outline:list-view',
            'componentPath': '/system/user/index.vue',
            'id': 111,
            'pid': 101,
        },
        {
            'name': 'cardList',
            'path': '/list/cardList',
            'meta.title': '菜单按钮',
            'meta.requiresAuth': true,
            'meta.icon': 'icon-park-outline:view-grid-list',
            'componentPath': '/system/menu/index.vue',
            'id': 121,
            'pid': 101,
        },
        {
            'name': 'roleList',
            'path': '/list/roleList',
            'meta.title': '角色管理',
            'meta.requiresAuth': true,
            'meta.icon': 'icon-park-outline:view-grid-list',
            'componentPath': '/system/role/index.vue',
            'id': 131,
            'pid': 101,
        },
        {
            'name': 'basic',
            'path': '/list/basic',
            'meta.title': '基础分析',
            'meta.requiresAuth': true,
            'meta.icon': 'icon-park-outline:view-grid-list',
            'componentPath': '/atmosphere/basicAnalysis/index.vue',
            'id': 131,
            'pid': 101,
        },
    ]
    flatMenu.forEach((item: any) => {
        const arr: any = {
            id: item.id,
            pid: item.pid,
            name: item.name,
            path: item.path,
            hide: item.hidden,
            requiresAuth: true,
            'meta.title': item.meta.title,
            'meta.requiresAuth': true,
            'meta.icon': item.meta.icon,
            'meta.menuType': item.component === 'Layout' ? 'dir' : 'page',
            'meta.hide': item.hidden,
            'meta.herf': item.meta.link,
            'meta.keepAlive': item.meta.noCache,
            'meta.withoutTab': true,
            'meta.pinTab': true,
            componentPath: item.component === 'Layout' ? null : item.component
        }
        rows.push(arr)
    })
    return rows;
}